const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: '43.143.253.188',
  user: 'nextjs',
  password: 'yEAiRE2jcxTyRBy5',
  database: 'nextjs',
  charset: 'utf8mb4'
};

async function verifySyncResult () {
  const connection = await mysql.createConnection(dbConfig);

  try {
    console.log('🔍 验证地图区域数据同步结果...\n');

    // 1. 验证1418指令的数据
    console.log('📋 验证1418指令数据（选择区域）:');

    const testCases1418 = [
      { areaId: 100, expected: ['许昌', '洛阳', '长安'] },
      { areaId: 19, expected: ['成都', '南中', '汉中'] },
      { areaId: 43, expected: ['建业', '长沙', '江陵'] }
    ];

    for (const testCase of testCases1418) {
      const [areas] = await connection.execute(
        'SELECT id, name FROM map_areas WHERE parent_id = ? ORDER BY id',
        [testCase.areaId]
      );

      console.log(`\n  areaId: ${testCase.areaId}`);
      console.log(`  期望: ${testCase.expected.join(', ')}`);
      console.log(`  实际: ${areas.map(a => a.name).join(', ')}`);

      if (areas.length === testCase.expected.length) {
        console.log(`  ✅ 数量匹配: ${areas.length} 个区域`);
      } else {
        console.log(`  ❌ 数量不匹配: 期望 ${testCase.expected.length}, 实际 ${areas.length}`);
      }

      // 显示详细信息
      areas.forEach(area => {
        console.log(`    ID: ${area.id} | ${area.name}`);
      });
    }

    // 2. 验证1436指令的数据
    console.log('\n📋 验证1436指令数据（获取站点信息）:');

    const testCases1436 = [
      { pid: 289, expected: ['新手村(1级)', '陈留(5级)', '九里山(20级)', '芒砀山(30级)'] },
      { pid: 302, expected: ['官渡(40级)', '虎牢关(50级)', '乌巢(80级)', '函谷关(55级)'] },
      { pid: 304, expected: ['华山(90级)', '潼关(75级)', '五丈原(65级)'] },
      { pid: 70, expected: ['落凤坡(30级)', '广汉(40级)', '峨眉山(50级)'] },
      { pid: 111, expected: ['云南(60级)', '泸水(70级)', '西洱湖(80级)'] },
      { pid: 117, expected: ['子午谷(90级)', '街亭(100级)', '上方谷(120级)'] },
      { pid: 154, expected: ['广陵'] },
      { pid: 212, expected: ['巴陵', '洞庭湖', '湘水(130级)'] },
      { pid: 264, expected: ['麦城(140级)', '长坂坡(140级)', '夷陵'] }
    ];

    for (const testCase of testCases1436) {
      const [areas] = await connection.execute(
        'SELECT id, name, extension FROM map_areas WHERE parent_id = ? ORDER BY id',
        [testCase.pid]
      );

      console.log(`\n  pid: ${testCase.pid}`);
      console.log(`  期望: ${testCase.expected.join(', ')}`);

      const actualNames = areas.map(a => {
        if (a.extension) {
          return `${a.name}${a.extension}`;
        }
        return a.name;
      });
      console.log(`  实际: ${actualNames.join(', ')}`);

      if (areas.length === testCase.expected.length) {
        console.log(`  ✅ 数量匹配: ${areas.length} 个区域`);
      } else {
        console.log(`  ❌ 数量不匹配: 期望 ${testCase.expected.length}, 实际 ${areas.length}`);
      }

      // 显示详细信息
      areas.forEach(area => {
        const fullName = area.extension ? `${area.name}${area.extension}` : area.name;
        console.log(`    ID: ${area.id} | ${fullName}`);
      });
    }

    // 3. 统计信息
    console.log('\n📊 最终统计信息:');
    const [totalCount] = await connection.execute('SELECT COUNT(*) as total FROM map_areas');
    const [parentCount] = await connection.execute('SELECT COUNT(*) as total FROM map_areas WHERE parent_id IS NOT NULL');
    const [extensionCount] = await connection.execute('SELECT COUNT(*) as total FROM map_areas WHERE extension IS NOT NULL AND extension != ""');

    console.log(`  总区域数: ${totalCount[0].total}`);
    console.log(`  有父级区域数: ${parentCount[0].total}`);
    console.log(`  有等级信息区域数: ${extensionCount[0].total}`);

    // 4. 检查是否有重复的ID
    console.log('\n🔍 检查ID重复情况:');
    const [duplicateIds] = await connection.execute(`
            SELECT id, COUNT(*) as count 
            FROM map_areas 
            GROUP BY id 
            HAVING COUNT(*) > 1
        `);

    if (duplicateIds.length === 0) {
      console.log('  ✅ 没有发现重复的ID');
    } else {
      console.log('  ❌ 发现重复的ID:');
      duplicateIds.forEach(dup => {
        console.log(`    ID ${dup.id}: ${dup.count} 次`);
      });
    }

    console.log('\n🎉 验证完成！');

  } catch (error) {
    console.error('❌ 验证失败:', error);
  } finally {
    await connection.end();
  }
}

// 运行脚本
if (require.main === module) {
  verifySyncResult().catch(console.error);
}

module.exports = { verifySyncResult }; 